/*************************************************
Name : UpdateAccount_WS
Created By : dthekumparampil
Created Date : Apr22, 2013
Description : An Apex REST service class created to recieve the incoming request for Account update.
*************************************************/
@RestResource(urlMapping='/UpdateAccount/*')
global with sharing class UpdateAccount_WS {

	/**
	Method Name : UpdateAccount 
	Created By : dthekumparampil
	Created Date : Apr22, 2013 
	Description : This method accepts input parameters and updates Account record.
	Input Parameters : String AccountID, String ColleagueID, String Birthdate, String AddressLine1, String AddressLine2, String AddressLine3, 
						String City, String State, String Zip, String Country, String Phone, String PersonalEmail, String UniversityEmail, 
						String Program, String OpportunityStatus, String Employer, String EmployerText, String PrimaryPaymentOption,String SecondaryPaymentOption
	Output Parameters : String responseStatus
	**/    

	@HttpPost
    global static String UpdateAccount(String AccountID, String ColleagueID, String Birthdate, String AddressLine1, String AddressLine2, String AddressLine3, 
										String City, String State, String Zip, String Country, String Phone, String PersonalEmail, String UniversityEmail, 
										String Program, String OpportunityStatus, String Employer, String EmployerText, 
										String PrimaryPaymentOption,String SecondaryPaymentOption){
    
    	/*Trimming all the Input strings*/
    	AccountID = AccountID.trim();
		ColleagueID = ColleagueID.trim();
		Birthdate = Birthdate.trim();
		AddressLine1 = AddressLine1.trim();
		AddressLine2 = AddressLine2.trim();
		AddressLine3 = AddressLine3.trim();
		City = City.trim();
		State = State.trim();
		Zip = Zip.trim();
		Country = Country.trim();
		Phone = Phone.trim();
		PersonalEmail = PersonalEmail.trim();
		UniversityEmail = UniversityEmail.trim();
		Program = EncodingUtil.urlDecode(Program.trim(),'UTF-8');
		OpportunityStatus = OpportunityStatus.trim();
		Employer = EncodingUtil.urlDecode(Employer.trim(),'UTF-8');
		EmployerText = EmployerText.trim();
		PrimaryPaymentOption  = PrimaryPaymentOption.trim();
		SecondaryPaymentOption = SecondaryPaymentOption.trim();
		
		/*Formatting Phone number*/
        if(Phone.length()>0){
	        DataFormatter formatter = new DataFormatter();
	        Phone = formatter.phoneFormatter(Phone);
	        if(Phone.contains('Failed')){
	        	return 'Failure: Please enter a valid phone number';
	        }
        }
		
		/*Getting RecordType IDs*/
        String accountRecordType = Schema.Sobjecttype.Account.getRecordTypeInfosByName().get('West - Student Account').getRecordTypeId();
        String programRecordType = Schema.Sobjecttype.Program__c.getRecordTypeInfosByName().get('West - Program').getRecordTypeId();
        String employerRecordType = Schema.Sobjecttype.Employer__c.getRecordTypeInfosByName().get('West').getRecordTypeId();
        
        /*Checking Salesforce Id in Account*/
        list<Account> accountInSalesforce;
    	accountInSalesforce = [SELECT id, Account_ID__c, Birth_Date__c,PersonMailingStreet, PersonMailingCity, PersonMailingState, PersonMailingPostalCode, 
    									PersonMailingCountry, PersonMobilePhone, PersonEmail, University_Email__pc, Program__c, Employer__pc, Employer_Text__pc 
    							FROM Account 
    						   	WHERE IsPersonAccount=true 
    						   		AND Account_Id__C=:AccountID 
    						   		AND RecordTypeId =: accountRecordType];
    	
    	if(accountInSalesforce.size()>0){
    		
    		Program__c programValue;
    		Employer__c employerValue;
    		Boolean employerMatchFound = true;
    		
    		try{
    			programValue = [SELECT id, name FROM Program__C WHERE name =: Program AND RecordTypeId=:programRecordType AND Active__c =: true LIMIT 1];	
    		}catch (Queryexception qE){
    			return 'Failure: No matching Program Database.';
    		}
    		try{
    			employerValue = [SELECT id,name FROM Employer__C WHERE name=: Employer AND RecordTypeId=: employerRecordType AND Active__c =:true LIMIT 1];	
    		}catch (Queryexception qE){
    			employerMatchFound = false;
    		}
    		
    		try{
    			String strMailingStreet =  AddressLine1 + ' ' +AddressLine2+ ' '+AddressLine3;
    			Date dtBirthDate = date.parse(Birthdate);
    			
    			accountInSalesforce[0].Birth_Date__c = dtBirthDate;
    			accountInSalesforce[0].PersonMailingStreet = strMailingStreet;
    			accountInSalesforce[0].PersonMailingCity = City;
    			accountInSalesforce[0].PersonMailingState = State;
    			accountInSalesforce[0].PersonMailingPostalCode = Zip;
    			accountInSalesforce[0].PersonMailingCountry = Country;
    			accountInSalesforce[0].PersonMobilePhone = Phone;
    			accountInSalesforce[0].PersonEmail = PersonalEmail;
    			accountInSalesforce[0].University_Email__pc = UniversityEmail;
    			accountInSalesforce[0].Program__c = programValue.id;
    			if(employerMatchFound){
    				accountInSalesforce[0].Employer__pc = employerValue.id;
    			}else{
    				accountInSalesforce[0].Employer_Text__pc = 	Employer + employerMatchFound ;
    			}
    			
    			update accountInSalesforce;
    			  
    		}catch(Exception e){
    			return 'Failure: Unable to modify Account';
    		}
    		
    		return 'Success: '+true;
    	}else{
    		return 'Failure: SalesforceID not found';	
    	}
    }

}